using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Windows;

namespace WebApplication1
{
    public partial class Register : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Submit_Click(object sender, EventArgs e)
        {
            string name = UserAccount.Text;
            string pwd = Password.Text;
            string gender = Gender_Female.Checked ? "1" : "0";
            string email = Email.Text;

            //sql字段
            string sql = "server =LAPTOP-4BDJKOV4; Initial Catalog = UserInfo; User Id = sa ; pwd = qazabc";

            //连接数据库
            SqlConnection con = null;

            //查询在数据库中是否存在相同的用户
            try
            {
                con = new SqlConnection(sql);
                //打开数据库连接
                con.Open();
                //对插入的数据进行查询
                //string sqlSentense = "select name from userInfo where name = '{0}'";
                //sqlSentense = string.Format(sql, name);
                //SqlCommand scom = new SqlCommand(sqlSentense, con);
                //int result = scom.ExecuteNonQuery();
                //if (result != 0)
                //{
                //    Literal1.Text = "用户名已存在！";
                //    return;
                //}


                string sqlAdd = "insert into userInfo values(@name,@pwd,@gender,@email)";


                SqlCommand comAdd = new SqlCommand(sqlAdd, con);


                SqlParameter[] paras =
                {
                    new SqlParameter("@name",name),
                    new SqlParameter("@pwd",pwd),
                    new SqlParameter("@gender",gender),
                    new SqlParameter("@email",email)

                };

                comAdd.Parameters.AddRange(paras);

                


                int result_add = comAdd.ExecuteNonQuery();
                if (result_add > 0)
                {
                    //在这里进行登录页面的跳转
                    
                    Literal1.Text = "注册成功！ <a href='Login.aspx'>去登录</a>";

                }
                else
                {
                    Literal1.Text = "注册失败";
                }




            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message);
            }
            finally
            {
                //断开连接
                if (con != null)
                {
                    con.Close();

                }
            }

        }
    }
}